﻿Public Class convertDTFromDb
    Public Shared Function convertDateTime(ByVal dtValue As Object) As DateTime
        If (dtValue Is DBNull.Value OrElse (dtValue Is Nothing) _
                     OrElse (dtValue.ToString().Trim() Is String.Empty)) Then
            Return Nothing
        Else
            Return CDate(dtValue)
        End If
    End Function

    Public Shared Function convertInt32(ByVal intValue As Object) As Int32
        If (intValue Is DBNull.Value OrElse (intValue Is Nothing) _
                     OrElse (intValue.ToString().Trim() Is String.Empty)) Then
            Return 0
        Else
            Return CInt(intValue)
        End If
    End Function

    Public Shared Function convertInt64(ByVal intValue As Object) As Int64
        If (intValue Is DBNull.Value OrElse (intValue Is Nothing) _
                     OrElse (intValue.ToString().Trim() Is String.Empty)) Then
            Return 0
        Else
            Return CLng(intValue)
        End If
    End Function

    Public Shared Function convertInt16(ByVal intValue As Object) As Int16
        If (intValue Is DBNull.Value OrElse (intValue Is Nothing) _
                     OrElse (intValue.ToString().Trim() Is String.Empty)) Then
            Return 0
        Else
            Return CShort(intValue)
        End If
    End Function

    Public Shared Function convertDouble(ByVal dbValue As Object) As Double
        If (dbValue Is DBNull.Value Or (dbValue Is Nothing) _
                     Or (dbValue.ToString().Trim() Is String.Empty) Or dbValue.ToString() = "") Then
            Return 0
        Else
            Return CDbl(dbValue)
        End If
    End Function

    Public Shared Function convertDoubleNothing(ByVal dbValue As Object) As Double
        If (dbValue Is DBNull.Value Or (dbValue Is Nothing) _
                     Or (dbValue.ToString().Trim() Is String.Empty) Or dbValue.ToString() = "") Then
            Return Nothing
        Else
            Return CDbl(dbValue)
        End If
    End Function

    Public Shared Function ConvertString(ByVal strValue As Object) As String
        If (strValue Is DBNull.Value) OrElse (strValue Is Nothing) _
                     OrElse (strValue.ToString().Trim() Is String.Empty) Then
            Return ""
        Else
            Return strValue.ToString()
        End If
    End Function

    Public Shared Function ConvertDecimal(ByVal decValue As Object) As Decimal
        If (decValue Is DBNull.Value) OrElse (decValue Is Nothing) _
                     OrElse (decValue.ToString().Trim() Is String.Empty) Then
            Return 0
        Else
            Return CDbl(decValue)
        End If
    End Function

    Public Shared Function ConvertLong(ByVal decValue As Object) As Long
        If (decValue Is DBNull.Value) OrElse (decValue Is Nothing) _
                     OrElse (decValue.ToString().Trim() Is String.Empty) Then
            Return 0
        Else
            Return CLng(decValue)
        End If
    End Function
    Public Shared Function ConvertBoolean(ByVal decValue As Object) As Boolean
        If (decValue Is DBNull.Value) OrElse (decValue Is Nothing) _
                     OrElse (decValue.ToString().Trim() Is String.Empty) Then
            Return 0
        Else
            Return CBool(decValue)
        End If
    End Function
    Public Shared Function ConvertShort(ByVal decValue As Object) As Short
        If (decValue Is DBNull.Value) OrElse (decValue Is Nothing) _
                     OrElse (decValue.ToString().Trim() Is String.Empty) Then
            Return 0
        Else
            Return CShort(decValue)
        End If
    End Function

    Public Shared Function GetByteArray(ByVal byteValue As Object) As Byte()
        If ((byteValue Is DBNull.Value) OrElse (byteValue Is Nothing)) Then
            Return Nothing
        Else
            Try
                Return DirectCast(byteValue, Byte())
            Catch ex As InvalidCastException
                Return Nothing
            End Try
        End If
    End Function

End Class
